package com.pictavore.gui

import com.pictavore.plugin.tool.*;
import com.pictavore.message.Messenger

import groovy.swing.SwingBuilder
import com.google.inject.Inject
import javax.swing.JPanel
import javax.swing.JComponent
import javax.swing.JFileChooser
import com.pictavore.image.PVImage
import com.pictavore.log.Logger

import com.pictavore.image.ImageMessage

/**
 *
 * @author caleb
 */
class FileLoaderToolPlugin implements ToolPlugin {
	@Inject
    Messenger messenger
    @Inject
    ImageView imageView

    @Inject
    Logger logger

    def panel

    String displayName = "File Loader"

    void load() {
        
    }

    JComponent getComponent() {
        if (panel == null) {
            def swing = new SwingBuilder()
            panel = swing.panel(background: java.awt.Color.BLUE) {
                button(action: action(name: 'Open File', closure: {
                    JFileChooser fc = new JFileChooser(null)
                    int returnVal = fc.showOpenDialog(null)

                    if (returnVal == JFileChooser.APPROVE_OPTION) {
                        File file = fc.selectedFile
                        //This is where a real application would open the file.
                        println "Opening: " + file.getName()
                        PVImage pvImage = new PVImage(file.absolutePath)
                        messenger.send(ImageMessage.NEW_IMAGE, this, pvImage)
                        logger.info("Loaded " + file.absolutePath)
                    }
                }))
            }
        }

        return panel
    }
}

